<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="申请处理" name="first">
        <el-table :data="applyList" border fit highlight-current-row>
          <el-table-column prop="applyTime" label="申请时间" width="180" />
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="email" label="邮箱" />
          <el-table-column label="状态" width="60">
            <template slot-scope="scope">
              <el-tag v-show="scope.row.userStatue === 0" type="success">正常</el-tag>
              <el-tag v-show="scope.row.userStatue === 1" type="danger">被封禁</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="重置密码" width="200" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="resetPassword(scope.row.id)">重置密码</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination :current-page="applyPage.apage" :page-size="applyPage.alimit" :total="applyPage.atotal"
          :page-sizes="[5,10, 20, 50]" style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper" @current-change="handleListCurrent"
          @size-change="handleListSize" />
      </el-tab-pane>
      <el-tab-pane label="处理历史" name="second">
        <el-table :data="historyList" border fit highlight-current-row>
          <el-table-column prop="dealTime" label="处理时间" width="180" />
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="email" label="邮箱" />
          <el-table-column prop="code" label="一次性登录码" width="280" />
          <el-table-column prop="applyTime" label="申请时间" width="160" />
          <el-table-column prop="dealTime" label="处理时间" width="160" />
          <el-table-column label="密码状态" width="120">
            <template slot-scope="scope">
              <el-tag v-show="scope.row.state === -1" type="warning">失效</el-tag>
              <el-tag v-show="scope.row.state === 0">未处理</el-tag>
              <el-tag v-show="scope.row.state === 1" type="info">未登录</el-tag>
              <el-tag v-show="scope.row.state === 2" type="success">已登录</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination :current-page="historyPage.hpage" :page-size="historyPage.hlimit" :total="historyPage.htotal"
          :page-sizes="[5,10, 20, 50]" style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange"
          @size-change="handleSizeChange" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import Apply from '@/api/apply.js'

export default {
  data () {
    return {
      applyList: null,
      applypage: {
        page: 1,
        limit: 5,
        total: 0
      },
      historyList: null,
      historyPage: {
        page: 1,
        limit: 5,
        total: 0
      },
      role: this.$store.getters.roles,
      activeName: 'first'
    }
  },

  created () {
    this.applypage = {
      apage: 1,
      alimit: 5,
      atotal: 0
    }
    this.historyPage = {
      hpage: 1,
      hlimit: 5,
      htotal: 0
    }
    this.getList()
  },
  methods: {
    // 获得列表
    getList () {
      const page = this.applypage.apage
      const limit = this.applypage.alimit
      Apply.getApplyList(page, limit)
        .then(response => {
          this.applyList = response.data.item
          this.applyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 获得处理过的记录
    getHistory () {
      const page = this.historyPage.hpage
      const limit = this.historyPage.hlimit
      Apply.getHistoryList(page, limit)
        .then(response => {
          this.historyList = response.data.item
          this.historyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 重置密码
    resetPassword (id) {
      this.$confirm('此操作将重置该用户密码, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        Apply.resetPassword(id)
          .then(response => {
            const pwd = response.data.code
            this.$alert('重置密码为 <strong>' + pwd + '</strong>', '重置密码成功', {
              dangerouslyUseHTMLString: true,
              confirmButtonText: '确定',
              callback: () => {
                this.getList()
              }
            })
          })
      }).catch(() => { })
    },
    // 分页
    handleListCurrent (val) {
      this.applyPage.apage = val
      this.getList()
    },
    handleListSize (val) {
      this.applyPage.alimit = val
      this.applyPage.apage = 1
      this.getList()
    },
    // 分页
    handleCurrentChange (val) {
      this.historyPage.hpage = val
      this.getHistory()
    },
    handleSizeChange (val) {
      this.historyPage.hlimit = val
      this.historyPage.hpage = 1
      this.getHistory()
    },

    handleClick (val) {
      if (val.name  === 'first') {
        this.getList()
      } else {
        this.getHistory()
      }
    },
    applyPage () {
      return this.applyPage
    }
  }
}
</script>
